//
//  LoginOfVC.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/16.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "LoginOfVC.h"
#import "Masonry.h"
#import "UMSocialData.h"
#import "UMSocial.h"
#import "UMSocialSnsService.h"
#import "UIImageView+WebCache.h"

@interface LoginOfVC ()<UMSocialUIDelegate>

@property(nonatomic, retain)UIImageView *imgView;

@end

@implementation LoginOfVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creat];
    
}

- (void)creat{
     self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fugu"]];
    
    self.imgView.frame = self.view.bounds;
    
    self.imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    [self.view insertSubview:self.imgView atIndex:0];
    
    [self.imgView release];
    
    
    UITextField *fildOfUserpassword = [[UITextField alloc]init];
    fildOfUserpassword.placeholder = @"   密码";
    fildOfUserpassword.backgroundColor = [UIColor whiteColor];
    fildOfUserpassword.alpha = 0.7;
    [self.view addSubview:fildOfUserpassword];
    fildOfUserpassword.layer.cornerRadius = 5;
    [fildOfUserpassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
        make.width.offset(SCREEN_WIDTH - 100);
        make.height.offset(35);
    }];
    [fildOfUserpassword release];
    
    UITextField *fildOfUserID = [[UITextField alloc]init];
    fildOfUserID.placeholder = @"   手机号/邮箱/用户名";
    fildOfUserID.backgroundColor = [UIColor whiteColor];
    fildOfUserID.alpha = 0.7;
    [self.view addSubview:fildOfUserID];
    fildOfUserID.layer.cornerRadius = 5;
    [fildOfUserID mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(fildOfUserpassword.mas_centerX);
        make.height.equalTo(fildOfUserpassword.mas_height);
        make.width.equalTo(fildOfUserpassword.mas_width);
        make.bottom.equalTo(fildOfUserpassword.mas_top).offset(-15);
    }];
    [fildOfUserID release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithRed:244.0 / 255.0 green:73.0 / 255.0 blue:81.0 / 255.0 alpha:0.6];
    button.layer.cornerRadius = 20;
    [button setTintColor:[UIColor whiteColor]];
    [button addTarget:self action:@selector(handleLogin:) forControlEvents:UIControlEventTouchUpInside];
    

    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(SCREEN_WIDTH / 4);
        make.height.offset(37);
        make.width.offset(58);
        make.top.equalTo(fildOfUserpassword.mas_bottom).offset(15);
     
    }];
    
    UILabel *lableOfOtherLogin = [[UILabel alloc]init];
    [self.view addSubview:lableOfOtherLogin];
    
    lableOfOtherLogin.textColor = [UIColor whiteColor];
    lableOfOtherLogin.text = @"使用其他方式登录";
    [lableOfOtherLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.offset(15);
        
        make.top.equalTo(button.mas_bottom).offset(55);
        
        
    }];
    lableOfOtherLogin.font = [UIFont systemFontOfSize:14];
    lableOfOtherLogin.textAlignment = NSTextAlignmentCenter;
    [lableOfOtherLogin release];
    
    

    
    UIView *viewOfLine = [[UIView alloc]init];
    [self.view addSubview:viewOfLine];
    viewOfLine.backgroundColor = [UIColor whiteColor];
    [viewOfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lableOfOtherLogin.mas_bottom).offset(15);
        make.height.offset(1);
        make.width.offset(SCREEN_WIDTH - 40);
        make.centerX.equalTo(self.view.mas_centerX);
    
    }];
    
    [viewOfLine release];
    
    UIButton *buttonOfBack = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOfBack.layer.cornerRadius = 18;
    buttonOfBack.backgroundColor = [UIColor redColor];
    buttonOfBack.alpha = 0.4;
    [buttonOfBack setTintColor:[UIColor whiteColor]];
    [self.view addSubview:buttonOfBack];
    [buttonOfBack mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(self.view.mas_leading).offset(15);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
        make.height.offset(30);
        make.width.offset(40);
        
        
    }];
    
    [buttonOfBack addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [buttonOfBack setImage:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
    
    for (int i = 0; i < 3; i++)
    {
        
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button2];
        button2.frame = CGRectMake(SCREEN_WIDTH / 4.7  +  i * SCREEN_WIDTH / 4,  SCREEN_HEIGHT -  SCREEN_HEIGHT / 4 + 30, 31, 31);
        
        
        
        
        [button2 addTarget:self action:@selector(handleThird:) forControlEvents:UIControlEventTouchUpInside];
//        button2.backgroundColor = [UIColor greenColor];
////        button2.layer.cornerRadius = 16;
        NSString *str = [NSString stringWithFormat:@"weibo%d", i];
        [button2 setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
        
        button2.imageView.layer.cornerRadius = 16;
        button2.tag = i;
        
    }
    
    
}

- (void)handleThird:(UIButton *)login{
    
    if (login.tag == 0)
    {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
//                NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
                
            }});
        [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
            NSLog(@"response is %@",response);
        }];

    }
    else if (login.tag == 1)
    {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
//                NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
                
                [self.imgView sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
            }});
        
        
        
        
        
        
    }
    else
    {
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
                
                [self.imgView sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
                
            }
            
        });
    }
    
    
    
    
    
    
}

#pragma mark - 撤销第一响应
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
    
    
}

#pragma mark - 登录键响应
- (void)handleLogin:(UIButton *)login{
    
    
    
    
    
}

#pragma mark - 返回键响应
- (void)handleBack:(UIButton *)back{
    
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}



@end
